<?php
namespace app\console\controller;

use app\console\model\ProtectType as FeedbackTypeModel;
use think\Request;
use think\Url;
use think\Db;
use think\Controller;
use think\Response;
use think\Loader;

class ProtectType extends Controller
{
    /*
     * 列表
     */
    public function index()
    {
        // 获取参数
        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];

        // 读取参数
        $data = FeedbackTypeModel::where($condition)->order(['sort' => 'asc', 'id' => 'asc'])->paginate(20, false, ['query' => $inquire]);

        return $this->fetch('index', [
            'mate_title' => '反馈分类列表',
            'data'       => $data
        ]);
    }


    /*
     * 添加
     */
    public function create()
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('FeedbackType');
            //if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new FeedbackTypeModel())->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }
        return $this->fetch('create', [
            "mate_title" => '反馈分类添加',

        ]);

    }

    /*
     * 删除
     */
    public function destroy($id)
    {

        if (FeedbackTypeModel::destroy($id)) {
            win('操作成功', Url::build('index'));
        }

        fail('操作失败');

    }

    /*
     * 编辑
     */
    public function update($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('FeedbackType');
           // if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new FeedbackTypeModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }

        $item = FeedbackTypeModel::get($id);

        return $this->fetch('create', [
            "mate_title" => '反馈分类编辑',
            'item'       => $item

        ]);
    }

    /*
     * 启用状态改变
     */
    public function auditing()
    {
        $id = Request::instance()->param('id');
        $status = FeedbackTypeModel::where('id', $id)->value('status') == '1' ? 2 : 1;
        if (FeedbackTypeModel::where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }

    /*
     * 排列顺序
     */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            FeedbackTypeModel::where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('FeedbackType/index'));
    }
}
